package com.zjdiepu.www.biz.partner;

import java.util.Date;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.BaseService;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.base.enums.type.ThirdModeIDType;
import com.zjdiepu.www.dao.partner.ThAuditinfoMapper;
import com.zjdiepu.www.ibiz.partner.IThAuditinfoService;
import com.zjdiepu.www.model.partner.dto.ThAuditinfo;
import com.zjdiepu.www.model.partner.ext.ThAuditinfoExt;
import com.zjdiepu.www.util.ExceptionHelper;

/**
 * 第三方审批信息Service层操作实现<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年09月25日 15时22分<br/>
 * @since 1.0<br/>
 */
@Service
public class ThAuditinfoServiceImpl extends BaseService implements IThAuditinfoService {
	public static final Logger logger = LoggerFactory.getLogger(ThAuditinfoServiceImpl.class);

	@Autowired
	private ThAuditinfoMapper thAuditinfoMapper;
	
	@Override
	public ThAuditinfo saveOrUpdateThAuditinfo(String busiId, String dataId,
			String busiType, String xeApplyNo, Integer approveStatus,
			String approveStatusName,String approveMessage, Date timeAudit,
			ThirdModeIDType thirdModeIDType, String currentUserId,
			String currentUserName) {
		ThAuditinfo thAuditinfoParam = new ThAuditinfo();
		thAuditinfoParam.setDpStatus(DpStatus.NORMAL.getCode());
		thAuditinfoParam.setBusiId(busiId);
		if(null == thirdModeIDType) {
			// 不可能为null，为null时必定参数传错，调试阶段必须发现
			ExceptionHelper.thow("9999", "保存错误处理时参数模块ID为空");
		}
		thAuditinfoParam.setThirdModeId(thirdModeIDType.getCode());
		ThAuditinfo thAuditinfo = thAuditinfoMapper.selectRefined(thAuditinfoParam);
		boolean insert = false;
		if(null == thAuditinfo) {
			insert = true;
			thAuditinfo = new ThAuditinfo();
			thAuditinfo.initInsertData(currentUserId, currentUserName);
		} else {
			thAuditinfo.initUpdateData(currentUserId, currentUserName);
		}
		thAuditinfo.setDataId(dataId);
		thAuditinfo.setBusiType(busiType);
		thAuditinfo.setBusiId(busiId);
		thAuditinfo.setThirdId(xeApplyNo);
		thAuditinfo.setThirdModeId(thirdModeIDType.getCode());
		thAuditinfo.setThirdModeName(thirdModeIDType.getValue());
		thAuditinfo.setApproveStatus(approveStatus);
		thAuditinfo.setApproveStatusName(approveStatusName);
		thAuditinfo.setApproveMessage(approveMessage);
		thAuditinfo.setTimeAudit(timeAudit);

		if(insert) {
			thAuditinfoMapper.insert(thAuditinfo);
		} else {
			thAuditinfoMapper.updateByPrimaryKeySelective(thAuditinfo);
		}
		return thAuditinfo;
	}
	
	@Override
	public void saveUpdateAndDelete(ThAuditinfo thAuditinfo, OperateType operType) {
		switch (operType) {
		case add:
			thAuditinfo.initInsertData(thAuditinfo.getModifier(), thAuditinfo.getModifierName());
			thAuditinfoMapper.insert(thAuditinfo);
			break;
		case edit:
			thAuditinfo.initUpdateData(thAuditinfo.getModifier(), thAuditinfo.getModifierName());
			// 影响的记录数
			int updateCount = thAuditinfoMapper.updateByPrimaryKeySelective(thAuditinfo);
			if(updateCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(updateCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		case delete:
			thAuditinfo.initDeleteData(thAuditinfo.getModifier(), thAuditinfo.getModifierName());
			// 影响的记录数
			int deleteCount = thAuditinfoMapper.updateByPrimaryKeySelective(thAuditinfo);
			if(deleteCount == 0){
				throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
			} else if(deleteCount > 1){
				throw new BaseException(ResultFlagType.MULTI_DATA.getCode().toString(),ResultFlagType.MULTI_DATA.getValue());
			}
			break;
		default:
			break;
		}
	}
	
	@Override
	public List<ThAuditinfo> selectByExt(ThAuditinfoExt thAuditinfoExt) {
		return thAuditinfoMapper.selectByExt(thAuditinfoExt);
	}
	
	@Override
	public int countByExt(ThAuditinfoExt thAuditinfoExt){
		return thAuditinfoMapper.countByExt(thAuditinfoExt);
	}
	
	@Override
	public ThAuditinfo selectById(String id) {
		return thAuditinfoMapper.selectByPrimaryKey(id);
	}
	
	@Override
	public void clearEmpty(Set<String> fieldNames, String id) {
		if(CollectionUtils.isNotEmpty(fieldNames)){
			thAuditinfoMapper.clearEmpty(fieldNames, id);
		}
	}
	
	@Override
	public int countRefined(ThAuditinfo thAuditinfo) {
		return thAuditinfoMapper.countRefined(thAuditinfo);
	}

	@Override
	public ThAuditinfo selectRefined(ThAuditinfo thAuditinfo) {
		return thAuditinfoMapper.selectRefined(thAuditinfo);
	}
	
	@Override
	public int deletedRefined(ThAuditinfo thAuditinfo) {
		return thAuditinfoMapper.deletedRefined(thAuditinfo);
	}

	@Override
	public boolean repeat(ThAuditinfo thAuditinfo) {
		int count = countRefined(thAuditinfo);
		if(count > 1) {
			return true;
		}
		if(count == 0) {
			return false;
		}
		
		if(StringUtils.isBlank(thAuditinfo.getId())) {
			return true;
		}
		ThAuditinfo dbItem = selectRefined(thAuditinfo);
		if(null == dbItem) {
			return false;
		}
		return !thAuditinfo.getId().equals(dbItem.getId());
	}
	
	@Override
	public int updateBatch(String modifier, String modifierName, Integer dpStatus, List<String> ids) {
		Integer current = (int)(System.currentTimeMillis() / 1000);
		return thAuditinfoMapper.updateBatch(modifier, modifierName, current, dpStatus, ids);
	}
	
	@Override
	public int insertBatch(List<ThAuditinfo> thAuditinfos) {
		return thAuditinfoMapper.insertBatch(thAuditinfos);
	}
}
